package com.antoco.operation.bigscreen.controller.modelcontroller.impl;

import com.antoco.operation.bigscreen.controller.modelcontroller.intf.SensorNumTjController;
import com.antoco.operation.bigscreen.model.dto.SensorNumTjDTO;
import com.antoco.operation.bigscreen.model.entity.SensorNumTj;
import com.antoco.operation.bigscreen.mapper.SensorNumTjMapper;
import com.antoco.operation.bigscreen.service.ChannelDataService;
import com.antoco.operation.bigscreen.service.autogeneration.intf.SensorNumTjService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/model/sensor-num-tj")
@RestController
public class SensorNumTjControllerImpl implements SensorNumTjController {

    @Autowired
    private SensorNumTjService sensorNumTjService;

    @Autowired
    private SensorNumTjMapper sensorNumTjMapper;

    @Autowired
    private ChannelDataService channelDataService;

    @Override
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public SensorNumTjDTO save(@Validated @RequestBody SensorNumTjDTO sensorNumTjDTO) {
        SensorNumTj sensorNumTj = sensorNumTjMapper.asEntity(sensorNumTjDTO);
        sensorNumTjService.save(sensorNumTj);
        channelDataService.pushAllLists(this.getClass().getName());
        return sensorNumTjDTO;
    }

    @Override
    @GetMapping("/{id}")
    public SensorNumTjDTO findById(@PathVariable("id") String id) {
        SensorNumTj sensorNumTj = sensorNumTjService.findById(id).orElse(null);
        return sensorNumTjMapper.asDTO(sensorNumTj);
    }

    @Override
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
        sensorNumTjService.deleteById(id);
        channelDataService.pushAllLists(this.getClass().getName());
    }

    @Override
    @GetMapping
    public List<SensorNumTjDTO> list() {
        return sensorNumTjMapper.asDTOList(sensorNumTjService.findAll());
    }

    @Override
    @GetMapping("/page-query")
    public Page<SensorNumTjDTO> pageQuery(Pageable pageable) {
        Page<SensorNumTj> sensorNumTjPage = sensorNumTjService.findAll(pageable);
        List<SensorNumTjDTO> dtoList = sensorNumTjPage
                .stream()
                .map(sensorNumTjMapper::asDTO).collect(Collectors.toList());
        return new PageImpl<>(dtoList, pageable, sensorNumTjPage.getTotalElements());
    }

    @Override
    @PutMapping("/{id}")
    public SensorNumTjDTO update(@RequestBody SensorNumTjDTO sensorNumTjDTO, @PathVariable("id") String id) {
        SensorNumTj sensorNumTj = sensorNumTjMapper.asEntity(sensorNumTjDTO);
        sensorNumTjMapper.asDTO(sensorNumTjService.update(sensorNumTj, id));
        channelDataService.pushAllLists(this.getClass().getName());
        return sensorNumTjDTO;
    }
}